package ch.fusun.baron.city;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import ch.fusun.baron.city.api.CityService;
import ch.fusun.baron.core.injection.Inject;
import ch.fusun.baron.data.DataUpdate;
import ch.fusun.baron.map.Tile;

/**
 * {@link CityService} data update
 */
public class CityDataUpdate implements DataUpdate {

	private Map<Tile, City> cityMap;

	@Inject
	private transient CityService cityService;

	/**
	 * Constructor
	 */
	public CityDataUpdate() {
		// for non-lethal injection
	}

	/**
	 * Constructor
	 * 
	 * @param cityMap
	 *            The map from tiles to cities
	 */
	public CityDataUpdate(Map<Tile, City> cityMap) {
		this.cityMap = cityMap;
	}

	@Override
	public void update() {
		List<City> cities = new LinkedList<City>();
		cities.addAll(cityService.getCities());
		for (City city : cities) {
			cityService.removeCity(city);
		}
		for (Tile tile : cityMap.keySet()) {
			cityService.setCity(tile, cityMap.get(tile));
		}
	}
}
